package com.scalar.chiptrack.commons.bean;

import java.util.ArrayList;
import java.sql.ResultSet;

/**
 * User: Ganesh
 * Date: Apr 29, 2008
 * Time: 11:24:50 AM
 */
public class IResultBeanImpl implements IResultBean {

    protected ArrayList rows = new ArrayList();

    protected int pageSize;
    protected int totalPageCount;
    protected int currentPage;
    protected int startIndex;
    protected int lastIndex;

    protected String sortColumn;
    protected boolean ascendingSortOrder;

    private static final int FIRST_PAGE = 1;

    public IResultBeanImpl(ResultSet rs)throws Exception
    {
        if (rs != null)
        {

        }
    }

    public IResultBeanImpl(ResultSet rs, int pageSize,
                           int totalPageCount, int currentPage, String sortColumn,
                           boolean ascendingSortOrder)throws Exception{
        this(rs);
        ResultRowManager rowManager = new ResultRowManager(rs);
        this.rows = rowManager.getRows();
        this.pageSize = pageSize;
        this.totalPageCount = totalPageCount;
        this.currentPage = currentPage;
        this.sortColumn = sortColumn;
        this.ascendingSortOrder = ascendingSortOrder;
    }

    public ArrayList getRows()
    {
        return rows;
    }

    /**
     * Get the total page count
     * @return int
     */
    public int getNumberOfPages()
    {
        return totalPageCount;
    }

    /**
     * get the current page number
     * @return int
     */
    public int getCurrentPage()
    {
        return currentPage;
    }

    /**
     * Check if its the first page
     * @return boolean
     */
    public boolean getIsFirstPage()
    {
        return (currentPage == FIRST_PAGE);
    }

    /**
     * Check if its the last page
     * @return boolean
     */
    public boolean getIsLastPage()
    {
        return (currentPage == totalPageCount);
    }

    /**
     * Get the column by which the result is sorted
     * @return String
     */
    public String getSortColumnName()
    {
        return sortColumn;
    }

    /**
     * Check if the sorting is ascending
     * @return boolean
     */
    public boolean getIsAscendingSort()
    {
        return ascendingSortOrder;
    }

    /**
     * Gets the first page number
     * @return int
     */
    public int getFirstPage()
    {
        return FIRST_PAGE;
    }

    /**
     * Gets the last page number
     * @return int
     */
    public int getLastPage()
    {
        return totalPageCount;
    }

    /**
     * Gets the previous page number
     * @return int
     */
    public int getPrevPage()
    {
        if (getIsFirstPage())
            return getFirstPage();

        return (currentPage - 1);
    }

    /**
     * Gets the next page number
     * @return int
     */
    public int getNextPage()
    {
        if (getIsLastPage())
            return getLastPage();

        return (currentPage + 1);
    }
}
